package org.anrc.poi;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class PoiOverlay extends Overlay {
	Cursor pois;
	ArrayList<GeoPoint> poiLocations;
	// private final int mRadius = 5;
	private Bitmap bubbleIcon;
	private Bitmap shadowIcon;
	private Paint innerPaint, borderPaint, textPaint;
	private MapView mapViewer;

	public PoiOverlay(Cursor cursor, MapView mapView) {
		super();
		pois = cursor;
		poiLocations = new ArrayList<GeoPoint>();
		refreshPoiLocations();

		this.mapViewer = mapView;

		pois.registerDataSetObserver(new DataSetObserver() {
			@Override
			public void onChanged() {
				refreshPoiLocations();
			}
		});
	}

	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		return false;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		Projection projection = mapView.getProjection();

		bubbleIcon = BitmapFactory.decodeResource(mapViewer.getResources(),
				R.drawable.bubble);
		shadowIcon = BitmapFactory.decodeResource(mapViewer.getResources(),
				R.drawable.shadow);

		for (GeoPoint geoPoint : poiLocations) {
			Point myPoint = new Point();
			projection.toPixels(geoPoint, myPoint);

			if (shadow == false) {
				canvas.drawBitmap(bubbleIcon, myPoint.x - bubbleIcon.getWidth()
						/ 2, myPoint.y - bubbleIcon.getHeight(), null);
			} else {
				canvas.drawBitmap(shadowIcon, myPoint.x, myPoint.y
						- shadowIcon.getHeight(), null);
			}
		}
	}

	private void refreshPoiLocations() {
		if (pois.moveToFirst())
			do {
				Double lat;
				lat = pois.getFloat(PoiProvider.LATITUDE_COLUMN) * 1E6;
				Double lng;
				lng = pois.getFloat(PoiProvider.LONGITUDE_COLUMN) * 1E6;
				GeoPoint geoPoint = new GeoPoint(lng.intValue(), lat.intValue());
				poiLocations.add(geoPoint);
			} while (pois.moveToNext());
	}

	public Paint getInnerPaint() {
		if (innerPaint == null) {
			innerPaint = new Paint();
			innerPaint.setARGB(225, 75, 75, 75); // gray
			innerPaint.setAntiAlias(true);
		}
		return innerPaint;
	}

	public Paint getBorderPaint() {
		if (borderPaint == null) {
			borderPaint = new Paint();
			borderPaint.setARGB(255, 255, 255, 255);
			borderPaint.setAntiAlias(true);
			borderPaint.setStyle(Style.STROKE);
			borderPaint.setStrokeWidth(2);
		}
		return borderPaint;
	}

	public Paint getTextPaint() {
		if (textPaint == null) {
			textPaint = new Paint();
			textPaint.setARGB(255, 255, 255, 255);
			textPaint.setAntiAlias(true);
		}
		return textPaint;
	}

}
